﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPMS.Log.Core
{
    /// <summary>
    /// 日志过滤器可连接口接口默认实现
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2009/10/9 16:37    创建
    /// </history>
    public sealed class LogFilterAttachableImplement : ILogFilterAttachable
    {
        #region ---ILogFilterAttachable

        /// <summary>
        /// 获得关联的所有日志过滤器
        /// </summary>
        /// <value>关联的所有日志过滤器</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 12:11    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 15:00    创建
        /// </history>
        public ILogFilter[] Filters
        {
            get
            {
                //判断是否缓存了日志过滤器
                if (null != this.m_CachedFilters)
                {
                    //如果已经缓存，则返回缓存的日志过滤器
                    return this.m_CachedFilters;
                }

                //同步
                lock (this)
                {
                    //判断是否缓存了日志过滤器
                    if (null != this.m_CachedFilters)
                    {
                        //如果已经缓存，则返回缓存的日志过滤器
                        return this.m_CachedFilters;
                    }

                    //生成日志缓存
                    this.m_CachedFilters = this.m_Filters.ToArray();
                }

                //返回缓存的日志过滤器
                return this.m_CachedFilters;
            }
        }

        /// <summary>
        /// 添加日志过滤器
        /// </summary>
        /// <param name="filter">要添加的日志过滤器</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 12:12    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 15:00    创建
        /// </history>
        public void AddFilter( ILogFilter filter )
        {
            //断言参数
            if (null == filter)
            {
                //如果为NULL，则直接返回
                return;
            }

            //清除日志过滤器
            lock (this)
            {
                //判断是否已经存在
                if (this.m_Filters.Contains( filter ))
                {
                    //如果过滤器已经存在，则直接返回
                    return;
                }

                //添加日志过滤器
                this.m_Filters.Add( filter );

                //清除日志缓存
                this.m_CachedFilters = null;
            }
        }

        /// <summary>
        /// 删除指定日志过滤器
        /// </summary>
        /// <param name="filter">要删除的日志过滤器</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 12:14    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 15:00    创建
        /// </history>
        public bool RemoveFilter( ILogFilter filter )
        {
            //用于保存删除结果
            bool isSuccess = false;

            //清除日志过滤器
            lock (this)
            {
                //删除日志过滤器
                isSuccess = this.m_Filters.Remove( filter );

                //清除日志缓存
                this.m_CachedFilters = null;
            }

            //返回删除结果
            return isSuccess;
        }

        /// <summary>
        /// 清除所有的日志过滤器
        /// </summary>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 12:17    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/25 15:00    创建
        /// </history>
        public void ClearFilters()
        {
            //清除日志过滤器
            lock (this)
            {
                //清除日志列表
                this.m_Filters.Clear();

                //清除日志缓存
                this.m_CachedFilters = null;
            }
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 日志过滤器
        /// </summary>
        private List<ILogFilter> m_Filters = new List<ILogFilter>();

        /// <summary>
        /// 缓存的日志过滤器
        /// </summary>
        private volatile ILogFilter[] m_CachedFilters;

        #endregion
    }
}
